package com.yupi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.question.QuestionAddRequest;
import com.yupi.project.model.dto.question.QuestionUpdateRequest;
import com.yupi.project.model.entity.Question;
import com.yupi.project.model.entity.Resourse;
import com.yupi.project.model.enums.FileDir;
import com.yupi.project.service.QuestionService;
import com.yupi.project.mapper.QuestionMapper;
import com.yupi.project.service.ResourseService;
import com.yupi.project.util.FileUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 * @description 针对表【question(问题表)】的数据库操作Service实现
 * @createDate 2023-07-12 11:17:00
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
        implements QuestionService {

    @Resource
    private ResourseService resourseService;

    @Resource
    private FileUtil fileUtil;

    @Override
    @Transactional
    public Long saveQuestion(QuestionAddRequest questionAddRequest) {
        Question question = new Question();
        BeanUtils.copyProperties(questionAddRequest, question);

        boolean save = save(question);

        if (save) {

//             如果存在图片保存
            List<MultipartFile> questionList = questionAddRequest.getQuestionList();
            List<MultipartFile> answerList = questionAddRequest.getAnswerList();

            Long id = question.getId();
            if (questionList != null && questionList.size() != 0) {
                saveResourse(id, 1, questionList);
            }

            if (answerList != null && questionList.size() != 0) {
                saveResourse(id, 2, answerList);
            }


            return id;
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "保存失败");
        }


    }

    @Override
    @Transactional
    public Long updateQuestion(QuestionUpdateRequest questionUpdateRequest) {
        Question question = new Question();
        BeanUtils.copyProperties(questionUpdateRequest, question);

        boolean save = updateById(question);

        if (save) {
//             如果存在图片保存
            List<MultipartFile> questionList = questionUpdateRequest.getQuestionList();
            List<MultipartFile> answerList = questionUpdateRequest.getAnswerList();

            Long id = question.getId();
            if (questionList != null && !questionList.isEmpty()) {
//                deleteResourse(id, 1);
                saveResourse(id, 1, questionList);
            }

            if (answerList != null && !answerList.isEmpty()) {
//                deleteResourse(id, 2);
                saveResourse(id, 2, answerList);
            }


            return id;
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "保存失败");
        }

    }

    @Override
    public Boolean deleteQuestion(Long id) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Question::getProductid, id);

        boolean b = removeById(id);

        if (b) {
            deleteResourse(id, 1);
            deleteResourse(id, 2);
        }

        return b;

    }


    /**
     * 保存资源
     */
    private void saveResourse(Long id, Integer statu, List<MultipartFile> list) {
        for (MultipartFile multipartFile : list) {
            String s = fileUtil.saveMultipartFile(multipartFile, FileDir.product);
            Resourse resourse = new Resourse();
            resourse.setUrl(s);
            resourse.setQuestionid(id);
            resourse.setStatu(statu);
            resourseService.save(resourse);
        }
    }

    /**
     * 删除资源
     */
    private void deleteResourse(Long id, Integer status) {
        LambdaQueryWrapper<Resourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Resourse::getQuestionid, id).eq(Resourse::getStatu, status);
        List<Resourse> list = resourseService.list(queryWrapper);
        List<String> urlList = list.stream().map(Resourse::getUrl).collect(Collectors.toList());
        if (urlList != null) {
            fileUtil.removeFileList(urlList);
        }
//        删除数据库记录
        resourseService.remove(queryWrapper);
    }
}




